package com.winexpress.account;

import com.winexpress.entity.Admin;
import com.winexpress.entity.Customer;
import com.winexpress.entity.User;
import com.winexpress.helper.AESEncryptor;
import com.winexpress.helper.Config;
import com.winexpress.session.account.AdminSessionBean;
import com.winexpress.session.account.CustomerSessionBean;
import com.winexpress.session.account.UserSessionBean;
import com.winexpress.util.EmailManager;
import com.winexpress.util.MD5;
import java.util.UUID;
import javax.ejb.EJB;
import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "AccountManagementWS")
public class AccountManagementWS {

    @EJB
    private CustomerSessionBean customerSessionBean;
    @EJB
    private UserSessionBean userSessionBean;
    @EJB
    private AdminSessionBean adminSessionBean;

    public static final String link = "https://www.winexpress6.tk/WineXpressClient/accountManagement/activateAccountResult?";

    @WebMethod(operationName = "registerCustomer")
    public String registerCustomer(@WebParam(name = "customer") Customer customer) throws Exception {
        String email = customer.getEmail();
        User user = (User) userSessionBean.getUserByEmail(AESEncryptor.encrypt(Config.seed, email));
        if (user != null) {
            return "The email has already been registered: \n" + customer.getEmail();
        }
        customer.setActivationCode(generatePassword());
        Long userID = customerSessionBean.registerNewCustomer(customer);
        if (userID != null) {
            String message = "Dear " + customer.getFirstName() + " " + customer.getLastName() + ",\n\n";
            message += "An new account has been created for you. Please click on the link below to activate your account:\n\n";
            message += link + "activationCode=" + customer.getActivationCode() + "&userID=" + String.valueOf(userID) + "\n\nBest regards, \nwineXpress";
            EmailManager emailManager = new EmailManager(email, "New Account Created for You - wineXpress", message);

            Thread thread = new Thread(emailManager);
            thread.start();
            return "<h3>New customer registered successfully</h3>. <br/><p>A confirmation email has been sent to " + email + " Please follow the instructions in the email to activate the account.</p>";
        } else {
            return "Fail to register new customer";
        }
    }

    @WebMethod(operationName = "registerAdmin")
    public String registerAdmin(@WebParam(name = "admin") Admin admin) throws Exception {
        String email = admin.getEmail();
        User user = (User) userSessionBean.getUserByEmail(email);
        if (user != null) {
            return "The email has already been registered: \n" + email;
        }
        String password = generatePassword().substring(0, 5);
        admin.setActivationCode(generatePassword());
        admin.setPassword(password);
        admin.setActivated(false);
        admin.setAdminUser(true);
        Long userID = adminSessionBean.registerNewAdmin(admin);
        if (userID != null) {
            String message = "Dear " + admin.getFirstName() + " " + admin.getLastName() + ",\n\n";
            message += "An new account has been created for you. Please click on the link below to activate your account:\n\n";
            message += link + "activationCode=" + admin.getActivationCode() + "&userID=" + String.valueOf(userID);
            message += "\n\nPassword generated for you: " + password;
            message += "\n\nPlease change your password after activating your account";
            message += "\n\nBest regards, \nwineXpress";
            EmailManager emailManager = new EmailManager(email, "New Account Created for You - wineXpress", message);

            Thread thread = new Thread(emailManager);
            thread.start();
            return "<h3>New admin registered successfully</h3>. <br/><p>A confirmation email has been sent to " + email + " Please follow the instructions in the email to activate the account.</p>";
        } else {
            return "Fail to register new admin";
        }
    }

    private String generatePassword() {
        return MD5.MD5(UUID.randomUUID().toString().replaceAll("-", "").substring(0, 7));
    }

    @WebMethod(operationName = "activateAccount")
    public Boolean activateAccount(@WebParam(name = "user") User user) throws Exception {
        return userSessionBean.activateAccount(user);
    }

    @WebMethod(operationName = "cancelAccount")
    public Boolean cancelAccount(@WebParam(name = "user") User user) throws Exception {
        return userSessionBean.cancelAccount(user);
    }

    @WebMethod(operationName = "updateAdminParticular")
    public Admin updateAdminParticular(@WebParam(name = "admin") Admin admin) throws Exception {
        return adminSessionBean.updateAdminParticular(admin);
    }

    @WebMethod(operationName = "updateCustomerParticular")
    public Customer updateCustomerParticular(@WebParam(name = "customer") Customer customer) throws Exception {
        return customerSessionBean.updateCustomerParticular(customer);
    }

    @WebMethod(operationName = "updatePassword")
    public Boolean updatePassword(@WebParam(name = "oldUser") User user) throws Exception {
        return userSessionBean.updatePassword(user);
    }

    @WebMethod(operationName = "forgetPassword")
    public String forgetPassword(@WebParam(name = "email") String email) throws Exception {
        User pendinguser = userSessionBean.getUserByEmail(email);
        if (pendinguser != null) {
            return userSessionBean.regeneratePassword(pendinguser);
        } else {
            return null;
        }
    }

    @WebMethod(operationName = "forgetPasswordEmail")
    public void forgetPasswordEmail(@WebParam(name = "user") User user) throws Exception {
        String message = "Dear customer" + ",\n\n";
        message += "We have received your request to reset your password, now your password has been reset to: " + "\n";
        message += user.getPassword() + "\n\n";
        message += "Thank you! \n";
        message += "Winexpress.com";
        EmailManager emailManager = new EmailManager(user.getEmail(), "New password generated - wineXpress", message);

        Thread thread = new Thread(emailManager);
        thread.start();
    }

    @WebMethod(operationName = "getCustomerByEmail")
    public Customer getCustomerByEmail(@WebParam(name = "email") String email) throws Exception {
        return customerSessionBean.getCustomerByEmail(email);
    }

    @WebMethod(operationName = "getCustomerById")
    public Customer getCustomerById(@WebParam(name = "id") Long id) {
        return customerSessionBean.getCustomerById(id);
    }

    @WebMethod(operationName = "deleteCustomerAccount")
    public Boolean deleteCustomerAccount(@WebParam(name = "id") Long id) throws Exception {
        return customerSessionBean.deleteCustomerAccount(id);
    }

 @WebMethod(operationName = "getFacebookCustomerAccount")
    public Customer getFacebookCustomerAccount(@WebParam(name = "facebookUser") Customer fbCustomer) throws Exception {
        Customer customer = customerSessionBean.getCustomerByFacebookId(fbCustomer.getFacebookID());
        if (customer != null) {
            return customer;
        }
        customerSessionBean.registerFacebookCustomerAccount(fbCustomer.getFacebookID(), fbCustomer.getFirstName(), fbCustomer.getLastName());
        return customerSessionBean.getCustomerByFacebookId(fbCustomer.getFacebookID());
    }

    @WebMethod(operationName = "getGoogleCustomerAccount")
    public Customer getGoogleCustomerAccount(@WebParam(name = "googleUser") Customer googleCustomer) throws Exception {
        Customer customer = customerSessionBean.getCustomerByGoogleId(googleCustomer.getGooogleID());
        if (customer != null) {
            return customer;
        }
        Long id = customerSessionBean.registerGoogleCustomerAccount(googleCustomer.getGooogleID(), googleCustomer.getFirstName(), googleCustomer.getLastName() , googleCustomer.getEmail());
        return customerSessionBean.getCustomerById(id);
    }

    @WebMethod(operationName = "getAdmin")
    public Admin getAdmin(@WebParam(name = "admin") Long id) throws Exception {
        return adminSessionBean.getAdminById(id);
    }

}
